{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chatbot\n", "\n", "## Try me\n", "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ffraile/computer_science_tutorials/blob/main/source/Extra%20Exercises/Ex7-%20Financebot%20(Solved).ipynb)[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/ffraile/computer_science_tutorials/main?labpath=source%2FExtra%20Exercises%2FEx7-%20Financebot%20(Solved).ipynb)\n", "\n", "Artificial Intelligence (AI) can be defined as the ability of a digital computer to perform tasks normally requiring human intelligence, like visual perception, speech recognition or chatting. A chatbot is a piece of software that may use AI and can interact with humans and keep a basic chat or conversation, very useful in companies to automate interactions with customers.\n", "\n", "Imagine that you run a company that provides financial services to buy and sell stocks and financial assets and you want to automate the customer service in your web and, at the same time, be more human friendly. Let’s create a chatbot!\n", "\n", "Follow the instructions bellow to create a basic chatbot in Python and submit a file (.py or .ipynb):\n", "\n", "\n", "1. Introduction and welcome.\n", "\n", "Initially, the bot has to ask the user’s name. Then, the user will input his/her name. Since we want a polite bot, the bot must print “Hello, {name}!” being {name} the name the user has input before. For example:\n", "\n", "Bot:\tWelcome, what’s your name?\n", "User:\tPeter\n", "Bot:\tHello, Peter!\n", "\n", "\n", "2. Listening to user’s requests.\n", "\n", "The bot will then ask “What do you want?” and wait for user input.\n", "\n", "The available commands that users can input are the following:\n", "- “a stock quote”\n", "- “buy a stock”\n", "- “sell a stock”\n", "- “goodbye”\n", "\n", "Depending on the command, the bot will perform different actions (see Point 3). After the action, the bot must start over asking again “What do you want?” and wait for user input.\n", "\n", "This behaviour is repeated in a loop except if the user input is “goodbye”. In this case, the bot will say “Goodbye, {name}!” and exit the program (being {name} the name the user has input in the introduction, see Point 1).\n", "\n", "\n", "3. Replying customer’s inquires.\n", "\n", "If the command input by the user is either “a stock quote” or “buy a stock” or “sell a stock”, the bot must ask “Which stock?” and wait for user to input one stock name or company name or ticker.\n", "\n", "Depending on the user response, the bot will answer attending to the following instructions:\n", "\n", "- A stock quote.\n", "A stock quote is the same as a stock price. The bot will answer with the stock price of the requested company. Don't worry, this bot is just a demo, it does not have to answer with the right price stock. Instead, your bot must answer a random integer number between 0 and 1000. You can use the python random library to obtain random numbers. Check the function randint with the proper parameters. You can check python documentation online or search on the Internet if you need so.\n", "\n", "For example:\n", "\n", "Bot:\tWhat do you want? \n", "User:\ta stock quote \n", "Bot:\tWhich stock? \n", "User:\tapple \n", "Bot:\t135$ \n", "\n", "- Buy a stock.\n", "The bot must answer with a confirmation, including the name of the company the user has just bought. \"You have bought {company} successfully\".\n", "\n", "For example:\n", "\n", "Bot:\tWhat do you want? \n", "User:\tbuy a stock \n", "Bot:\tWhich stock? \n", "User:\tapple \n", "Bot:\tYou have bought apple successfully \n", "\n", "- Sell a stock.\n", "The bot must answer with a confirmation, including the name of the company the user has just sold. \"You have sold {company} successfully\".\n", "\n", "For example:\n", "\n", "Bot:\tWhat do you want? \n", "User:\tsell a stock \n", "Bot:\tWhich stock? \n", "User:\tapple \n", "Bot:\tYou have sold apple successfully \n", "\n", "\n", "4. Other cases.\n", "\n", "When the bot asks for user input, if the user input is none of the commands described in Point 2, the bot must answer “Sorry, I don’t understand” and start over again and continue listening to user’s requests by asking “What do you want?” and waiting for another user input.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "print(\"Welcome, what's your name?\")\n", "name = input()\n", "print(f\"Hello, {name}!\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "print(\"Welcome, what's your name?\")\n", "name = input()\n", "print(f\"Hello, {name}!\")\n", "\n", "while (True):\n", " print(\"What do you want?\")\n", " command = input()\n", " if command == \"goodbye\":\n", " print(f\"Goodbye, {name}!\")\n", " break" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random\n", "\n", "\n", "print(\"Welcome, what's your name?\")\n", "name = input()\n", "print(f\"Hello, {name}!\")\n", "\n", "\n", "def quote():\n", " print(\"Which product?\")\n", " stock = input()\n", " price = random.randint(0, 1000)\n", " print(f\"{price}$\")\n", " \n", " \n", "def buy():\n", " print(\"Which product?\")\n", " stock = input() \n", " print(f\"You have bought {stock} successfully\")\n", " \n", " \n", "def sell():\n", " print(\"Which product?\")\n", " stock = input() \n", " print(f\"You have sold {stock} successfully\")\n", "\n", " \n", "while (True):\n", " print(\"What do you want?\")\n", " command = input()\n", " if command == \"goodbye\":\n", " print(f\"Goodbye, {name}!\")\n", " break\n", " elif command == \"a stock quote\":\n", " quote()\n", " elif command == \"buy a stock\":\n", " buy()\n", " elif command == \"sell a stock\":\n", " sell()\n", " else:\n", " print(\"Sorry, I don’t understand\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.4" } }, "nbformat": 4, "nbformat_minor": 4 }